const windowWidth = wx.getSystemInfoSync().windowWidth
const user_id = wx.getStorageSync('user_id')
const domainName = getApp().globalData.domainName
const oss_domain = getApp().globalData.oss_domain
const parameter = getApp().globalData.parameter
const status_bgColors = getApp().globalData.status_bgColors
const pilot_bgColors = getApp().globalData.pilot_bgColors
const status_list = getApp().globalData.status_list
const pilot_shortNames = getApp().globalData.pilot_shortNames



const update_singleStu_status=(that, index, set_status)=>{
  let jg_stu_id = that.data.stu_list[index].jg_stu_id
  wx.request({
    url: domainName + '/studentsIndex_update_stuStatus',
    data: {
      jg_stu_id:jg_stu_id,
      set_status:set_status,
    },
    ...parameter,
    success: res => {
      if(res.data.update_res){
        wx.showToast({
          title: '更新完成',
          duration: 1500,
          icon: 'success',
        })

        // let status = 'stu_list['+index+'].status'
        that.setData({
          ['stu_list['+index+'].status']:set_status,// 更新前端该学员状态
          stu_status_list:res.data.stu_status_list,// 更新学员状态统计数据
          show_page:false,
        })
      }
    },
  })
}
// 加载我的学生
const students_request=(that,formData,page_no)=>{
    wx.showNavigationBarLoading()
    wx.showLoading({
      title: '加载50人/次',
    })
    wx.request({
      url: domainName + '/filter_students',
      data: {
        formData:JSON.stringify(formData),
        page_no,
      }, ...parameter,
      success: res => {
        wx.hideNavigationBarLoading()
        wx.hideLoading()
        wx.stopPullDownRefresh()
        let stu_list = res.data.stu_list
        let stu_total_num = res.data.stu_total_num
        if(page_no){
          that.setData({
            stu_list: that.data.stu_list.concat(stu_list),
            page_no:that.data.page_no + 1,
          })
        }else{
          that.setData({
            page_no:1,
            stu_list:stu_list,
          })
        }
        if(stu_list.length==0){
          that.setData({
            no_more:true
          })
        }

        setTimeout(() => {
          wx.setNavigationBarTitle({
            title:  '学员（' + that.data.stu_list.length + '/' + stu_total_num + '）',
          })
        }, 500);
        return
      },
      fail:err=>{
        getApp().timeout_reconnect(err.errMsg,that)
      }
    })
  }
// 初始化，加载列表
const page_init=(that,jg_id,user_id)=>{
    wx.request({
      url: domainName + '/students_index_init',
      data: {
        jg_id,
        user_id,
      }, ...parameter,
      success: res => {
        // console.log('page_init',res.data)
        that.setData({
          loading:false,
          right_limit:res.data.right_limit,

          jg_StuLabel_list: res.data.jg_StuLabel_list,
          stu_status_list : res.data.stu_status_list,
          ins_list : res.data.ins_list,

          examInfo_list : res.data.examInfo_list,
          license_species_list : res.data.license_species_list,
          uav_class_list : res.data.uav_class_list,
          uav_level_list : res.data.uav_level_list,
          rank_list : res.data.rank_list,
        })
      },
    })
  }
// const status_id = [1,3,6,7,8,9,10]

const package_formData=(that, page_no)=>{
  // 整理回传数据
  let formData = {
    jg_id:that.jg_id,
    search_content:that.data.search_content,
    select_ins_id:that.data.select_ins_id,
    select_label_id:that.data.select_label_id,
    select_status_id:that.data.select_status_id,
    select_license_id:that.data.select_license_id,
    select_uavClass_id:that.data.select_uavClass_id,
    select_uavLevel_id:that.data.select_uavLevel_id,
    select_rank_id:that.data.select_rank_id,
    select_exam_id:that.data.select_exam_id,
  }
  students_request(that, formData, page_no) // 发送加载学员列表

}
const data_init=that=>{
    that.setData({
      page_no:0,
      no_more:false,

      search_content:'',
      select_ins_id:null,
      select_label_id:null,
      select_status_id:'',
      select_license_id:null,
      select_uavClass_id:null,
      select_uavLevel_id:null,
      select_rank_id:'',
    })
  }

  // 输入搜索内容
const input_search_content = (that, e)=>{
  that.setData({
    search_content:e.detail.value
  })
}
const go_search=(that)=>{
  getApp().get_vibration()
  let page_no = 0
  package_formData(that, page_no)
}
const clear_search=(that)=>{
  if(that.data.search_content){
    getApp().get_vibration()
    that.setData({
      search_content:''
    })
    let page_no = 0
    package_formData(that, page_no)
  }
}
  // 设置机构标签归类
const edit_label=(that,e)=>{
  wx.navigateTo({
    url: '/pages/jgs/jg_labels/jg_labels?jg_id=' + that.jg_id,
  })
  }
  /**
 * 责任教员
 */
const select_ins_setDate=(that,select_ins_id)=>{
  // 设置前端参数
  if(that.data.select_ins_id==select_ins_id){
    select_ins_id = null
  }
  that.setData({
    select_ins_id:select_ins_id,
    no_more: false
  })
  let page_no = 0
  package_formData(that, page_no) //向后端请求加载学员列表
}
const select_ins=(that,e)=>{
  getApp().get_vibration()
  var select_ins_id = e.currentTarget.id
  select_ins_setDate(that,select_ins_id)
}
const clear_selectIns=(that)=>{
  if(that.data.select_ins_id){
    getApp().get_vibration()
    let select_ins_id = null
    select_ins_setDate(that,select_ins_id)
  }
}
const edit_ins=(that)=>{
  getApp().get_vibration()
  wx.navigateTo({
    url: '/pages/jgs/jg_job/jg_job?jg_id=' + that.jg_id,
  })
}

const clear_select_label=(that)=>{
  if(that.data.select_label_id){
    getApp().get_vibration()
    let select_label_id = null
    that.setData({
      select_label_id,
      no_more: false
    })
    let page_no = 0
    package_formData(that, page_no)
  }
}
/**
* 考试情况
*/
const select_exam=(that,e)=>{
  getApp().get_vibration()

  let select_exam_id = e.currentTarget.id
  if(that.data.select_exam_id==select_exam_id){
    select_exam_id = null
  }
  that.setData({
    select_exam_id,
    no_more: false
  })
  let page_no = 0
  package_formData(that, page_no)
}
const clear_select_exam=(that)=>{
  if(that.data.select_exam_id){
    getApp().get_vibration()
    let select_exam_id = null
    that.setData({
      select_exam_id,
      no_more: false
    })
    let page_no = 0
    package_formData(that, page_no)
  }
}

/**
* 学员状态
*/
const select_stu_status=(that,e)=>{
  getApp().get_vibration()

  let select_status_id = e.currentTarget.id
  if(that.data.select_status_id==select_status_id){
    select_status_id = null
  }
  that.setData({
    select_status_id: select_status_id,
    no_more: false
  })
  let page_no = 0
  package_formData(that, page_no)
}
const clear_select_status=(that)=>{
  if(that.data.select_status_id){
    getApp().get_vibration()
    let select_status_id = null
    that.setData({
      select_status_id: select_status_id,
      no_more: false
    })
    let page_no = 0
    package_formData(that, page_no)
  }
}
/**
* 执照种类
*/
const select_license=(that,e)=>{
  getApp().get_vibration()
  let select_license_id = e.currentTarget.id
  let page_no = 0
  if(that.data.select_license_id==select_license_id){
    select_license_id = null
  }
  that.setData({
    select_license_id: select_license_id,
    no_more: false
  })
  package_formData(that, page_no)
}
const clear_license=(that)=>{
  if(that.data.select_license_id){
    getApp().get_vibration()
    let select_license_id = null
    let page_no = 0
    that.setData({
      select_license_id: select_license_id,
      no_more: false
    })
    package_formData(that, page_no)
  }
}
/**
* 类别等级
*/
const select_uav_class=(that,e)=>{
  getApp().get_vibration()
  let select_uavClass_id = e.currentTarget.id
  let page_no = 0
  if(that.data.select_uavClass_id==select_uavClass_id){
    select_uavClass_id = null
  }
  that.setData({
    select_uavClass_id: select_uavClass_id,
    no_more: false
  })
  package_formData(that, page_no)
}
const clear_uac_class=(that)=>{
  if(that.data.select_uavClass_id){
    getApp().get_vibration()

    let select_uavClass_id = null
    let page_no = 0
    that.setData({
      select_uavClass_id: select_uavClass_id,
      no_more: false
    })
    package_formData(that, page_no)
  }
}
/**
* 级别等级
*/
const select_uav_level=(that,e)=>{
  getApp().get_vibration()

  let select_uavLevel_id = e.currentTarget.id
  let page_no = 0
  if(that.data.select_uavLevel_id==select_uavLevel_id){
    select_uavLevel_id = null
  }
  that.setData({
    select_uavLevel_id: select_uavLevel_id,
    no_more: false
  })
  package_formData(that, page_no)
}
const clear_uavLevel=(that)=>{
  if(that.data.select_uavLevel_id){
    getApp().get_vibration()

    let select_uavLevel_id = null
    let page_no = 0
    that.setData({
      select_uavLevel_id: select_uavLevel_id,
      no_more: false
    })
    package_formData(that, page_no)
  }
}
/**
* 超视距等级
*/
const select_rank=(that,e)=>{
  getApp().get_vibration()
  let select_rank_id = e.currentTarget.id
  let page_no = 0
  if(that.data.select_rank_id==select_rank_id){
    select_rank_id = null
  }
  that.setData({
    select_rank_id: select_rank_id,
    no_more: false
  })
  package_formData(that, page_no)
}
const clear_rank=(that)=>{
  if(that.data.select_rank_id){
    getApp().get_vibration()

    let select_rank_id = null
    let page_no = 0
    that.setData({
      select_rank_id: select_rank_id,
      no_more: false
    })
    package_formData(that, page_no)
  }
}  
// 查看当天的试卷
const testPaper_library=(that)=>{
  getApp().get_vibration()
  if(['is_admin',1,2,3,4].indexOf(that.data.right_limit)==-1){
    wx.showModal({
      showCancel: false,
      title: '您暂无该权限',
    })
    return 
  }
  wx.navigateTo({
    url: '/pages/doTi/testPaper_library/testPaper_library?jg_id=' + that.jg_id,
  })
}
const settings=(that)=>{
  getApp().get_vibration()
  if(['is_admin',1,2].indexOf(that.data.right_limit)==-1){
    wx.showModal({
      showCancel: false,
      title: '您暂无该权限',
    })
    return
  }
  wx.navigateTo({
    url: '/pages/students/stu_settings/stu_settings?jg_id=' + that.jg_id
  })
}
// 查看训练情况
const view_dailyRec=(that)=>{
  getApp().get_vibration()
  wx.navigateTo({
    url: '/pages/students/daily_records/daily_records?jg_id=' + that.jg_id,
    fail:err=>{
      console.log(err)
    }
  })
}
  // 邀请学生
  const  add_stu=(that)=>{
    getApp().get_vibration()
    if(['is_admin',1,2].indexOf(that.data.right_limit)==-1){
      wx.showModal({
        showCancel: false,
        title: '您暂无该权限',
      })
      return
    }
    wx.navigateTo({
      url:'/pages/students/stu_register/create_invitation/create_invitation?jg_id=' + that.jg_id,
    })
  }
// 查看考试记录
const  exam_records=(that)=>{
  getApp().get_vibration()

  wx.showActionSheet({
    itemList: ['查看考试记录'],
    success: res => {
      let tapIndex = res.tapIndex
      if(tapIndex==0){
        wx.navigateTo({
          url: '/pages/students/exam/exam_history/exam_history?jg_id=' + that.jg_id,
        })
      }
    },
  })
}  

// pages/my_students/my_students.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    domainName,
    pilot_bgColors,
    status_bgColors,
    windowWidth,
    status_list,
    pilot_shortNames,
    icon_url:oss_domain + 'static/picture/app_icon/',
    stu_list:[],
    loading:true,
    license_species_list:[],
    uav_class_list:[],
    uav_level_list:[],
    rank_list:[],
  },

  // 底部导航栏 - 操作
  view_dailyRec(){ view_dailyRec(this)},
  add_stu(){ add_stu(this)},
  settings(){ settings(this)},
  testPaper_library(){ testPaper_library(this)},
  exam_records(){ exam_records(this)},

  // 搜索框
  input_search_content(e){ input_search_content(this,e)},
  go_search(){ go_search(this)},
  clear_search(){ clear_search(this)},
  

  // 筛选学员
  select_ins(e){ select_ins(this, e)},
  clear_selectIns(e){ clear_selectIns(this, e)},
  edit_ins(e){ edit_ins(this, e)},

//  /* 分组/批次 */
select_label(e){
  getApp().get_vibration()


  let select_label_id = e.currentTarget.id
  if(this.data.select_label_id==select_label_id){
    select_label_id = null
  }
  this.setData({
    select_label_id,
    no_more: false
  })
  let page_no = 0
  package_formData(this, page_no)
},
  clear_select_label(e){ clear_select_label(this, e)},
  edit_label(e){ edit_label(this, e)},
  //筛选学员状态
  select_stu_status(e){ select_stu_status(this, e)},
  clear_select_status(e){ clear_select_status(this, e)},
  //筛选考试情况状态
  select_exam(e){ select_exam(this, e)},
  clear_select_exam(e){ clear_select_exam(this, e)},
  // 筛选执照种类
  select_license(e){ select_license(this, e)},
  clear_license(e){ clear_license(this, e)},
  // 筛选类别等级
  select_uav_class(e){ select_uav_class(this, e)},
  clear_uac_class(e){ clear_uac_class(this, e)},
  // 筛选级别等级
  select_uav_level(e){ select_uav_level(this, e)},
  clear_uavLevel(e){ clear_uavLevel(this, e)},
  // 筛选操控员等级
  select_rank(e){ select_rank(this, e)},
  clear_rank(e){ clear_rank(this, e)},
  // 点击学员
  tap_stu(e){
    getApp().get_vibration()

    var the_stu_index = e.currentTarget.id
    this.data.the_stu_index = the_stu_index
    let the_stu_status = this.data.stu_list[the_stu_index].status
    if(['is_admin',1,2].indexOf(this.data.right_limit)>-1 && the_stu_status==1){
      wx.showActionSheet({
        itemList: ['批量审核','查看/审核信息'],
        itemColor: '#00b050',
        success: res => {
          if(res.tapIndex==0){
            wx.navigateTo({
              url: '/pages/students/stu_register/check_stu_in/check_stu_in?jg_id=' + this.jg_id,
            })
          }else{
            this.view_register_info()
          }
        },
      })

      return 
    }else{
      this.setData({
        show_page:true,
        the_stu_index,
      })
    }
  },

  hide_show(){getApp().get_vibration();this.setData({show_page:false})},

  
  view_stu_ranking(){
    getApp().get_vibration()
    wx.navigateTo({
      url:'/pages/students/rank_mine_index/rank_mine_index?jg_stu_id=' + this.data.stu_list[this.data.the_stu_index].jg_stu_id
    }) 
  },
  // 查看章节
  view_chap(){
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/students/stu_doti_of_chap/stu_doti_of_chap?jg_stu_id=' + this.data.stu_list[this.data.the_stu_index].jg_stu_id + '&stu_name=' + this.data.stu_list[this.data.the_stu_index].name,
    })
  },

  // 预测考分
  predict_score(){
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/students/predict_ExamScore/predict_ExamScore?jg_stu_id=' + this.data.stu_list[this.data.the_stu_index].jg_stu_id,
    })
  },

  // 学员报名信息
  view_register_info(){
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/students/stu_register/stuRegister_info/stuRegister_info?jg_stu_id=' + this.data.stu_list[this.data.the_stu_index].jg_stu_id + '&page_index=' + this.data.the_stu_index,
    })
  },

  set_examInfo(){
    let stu_info = this.data.stu_list[this.data.the_stu_index]
    wx.navigateTo({
      url: '/pages/students/exam/set_examInfo/set_examInfo?stu_info=' + encodeURIComponent(JSON.stringify(stu_info)),
    })
  },

  // 设置学员状态
  set_stu_status(e){
    getApp().get_vibration()
    let set_status = e.currentTarget.id
    update_singleStu_status(this, this.data.the_stu_index, set_status)
  },

  stu_passing_rate(){
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/students/passing_rate/passing_rate?jg_id=' + this.jg_id
    })
  },

  training_monitor(){
    getApp().get_vibration()
    wx.navigateTo({
      url: '/pages/students/training_monitor/training_monitor?jg_id=' + this.jg_id
    })
  },


  batch_page(){
    getApp().get_vibration()
    if(['is_admin',1,2].indexOf(this.data.right_limit)==-1){
      wx.showModal({
        showCancel: false,
        title: '您暂无该权限',
      })
      return
    }
    wx.navigateTo({
      url: '/pages/students/batch_page/batch_page?jg_id=' + this.jg_id,
    })
  },


  refresh(){
    if(!off_vibration){
      getApp().get_vibration()
    }
    this.onLoad(this.options)
  },





  /**
   * 生命周期函数--监听页面加载
   */


  onLoad: function (options) {
    // console.log('options',options)
    let jg_id = options.jg_id
    this.setData({jg_id})
    this.jg_id = jg_id
    this.jg_name = options.jg_name
    data_init(this)  // 初始化数据
    page_init(this,this.jg_id,user_id) // 加载页面基础数据
    package_formData(this,0)  // 加载50名未过滤学员列表
  },

  onPullDownRefresh(){
    // this.onLoad(this.options)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.can_onShow){
      package_formData(this,0)  // 加载50名未过滤学员列表
      this.can_onShow=false
    }
  },

  onReady(){
    this.setData({
      windowHeight: wx.getSystemInfoSync().windowHeight,
    })
  },

  onHide(){

  },

  onPullDownRefresh(){
    data_init(this)  // 初始化数据
    page_init(this,this.jg_id,user_id) // 加载页面基础数据
    package_formData(this,0)  // 加载20名未过滤学员列表
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if(!this.data.no_more){
      package_formData(this,this.data.page_no)  // 加载20名未过滤学员列表
    }
  },

})